import utils from "@common/utils/utils";

export default {
	async checkRow(_vm, {
		type, row, selectionList, label,
		showField = 'code',
		auditField = 'audit_state', auditCheck = 1,
		cancelField = 'state', cancelCheck = 0,
		method,
	}) {
		if (type === 'detail') {
			return row || {};
		}
		if (type === 'add') {
			return row || {};
		}
		if (type === 'edit') {
			if (!row && !_vm.checkSelection(selectionList, false)) {
				return;
			}
			if (!row && utils.isNotEmptyList(selectionList)) {
				row = $.extend({}, selectionList[0]);
			}
			if (utils.isNotEmpty(cancelField) && row[cancelField] === cancelCheck) {
				_vm.$message.error(`${ label }：${ row[showField] } 已作废，无法编辑！`);
				return;
			}
			if (utils.isNotEmpty(auditField) && row[auditField] === auditCheck) {
				_vm.$message.error(`${ label }：${ row[showField] } 已审核，请先反审核再编辑！`);
				return;
			}
			return row;
		}
		if ([ 'audit', 'reAudit' ].indexOf(type) !== -1) {
			if (!row && !_vm.checkSelection(selectionList, true)) {
				return;
			}
			if (row) {
				selectionList = [row];
			}
			if (utils.isNotEmpty(cancelField)) {
				let filter = selectionList.filter(item => item[cancelField] === cancelCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					_vm.$message.error(`${ label }：${ showList.join('、') } 已作废，无法${ type === 'audit' ? '审核' : '反审' }！`);
					return;
				}
			}
			if (utils.isNotEmpty(auditField)) {
				let filter = selectionList.filter(item => item[auditField] === auditCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					if (type === 'audit') {
						_vm.$message.error(`${ label }：${ showList.join('、') } 已审核`);
					} else {
						_vm.$message.error(`${ label }：${ showList.join('、') } 未审核，无法反审`);
					}
					return;
				}
			}
			return selectionList.map(item => $.extend({}, item));
		}
		if (type === 'delete') {
			if (!row && !_vm.checkSelection(selectionList, true)) {
				return;
			}
			if (row) {
				selectionList = [row];
			}
			if (typeof method === 'function') {
				if (!method(selectionList)) {
					return;
				}
			}
			let msg = `是否删除该${ label }？`;
			if (!row) {
				msg = `是否删除选择的${ label }？`;
			}
			let confirm = await _vm.$confirm(msg, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			}).then(() => {
				return true;
			}).catch(() => {
				return false;
			});
			if (!confirm) {
				return;
			}
			return selectionList.map(item => $.extend({}, item));
		}
		if (type === 'apply') {
			if (!row && !_vm.checkSelection(selectionList, true)) {
				return;
			}
			if (row) {
				selectionList = [row];
			}
			if (utils.isNotEmpty(auditField)) {
				let filter = selectionList.filter(item => item[auditField] === auditCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					_vm.$message.error(`${ label }：${ showList.join('、') } 未审核，无法申请收票`);
					return;
				}
			}
			if (utils.isNotEmpty(cancelField)) {
				let filter = selectionList.filter(item => item[cancelField] === cancelCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					_vm.$message.error(`${ label }：${ showList.join('、') } 已作废，无法申请收票`);
					return;
				}
			}
			return selectionList.map(item => $.extend({}, item));
		}
		if ([ 'confirm', 'reConfirm' ].indexOf(type) !== -1) {
			if (!row && !_vm.checkSelection(selectionList, true)) {
				return;
			}
			if (row) {
				selectionList = [row];
			}
			if (utils.isNotEmpty(auditField)) {
				let filter = selectionList.filter(item => item[auditField] === auditCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					_vm.$message.error(`${ label }：${ showList.join('、') } 未审核，无法${ type === 'confirm' ? '' : '反' }确认`);
					return;
				}
			}
			if (utils.isNotEmpty(cancelField)) {
				let filter = selectionList.filter(item => item[cancelField] === cancelCheck);
				if (utils.isNotEmptyList(filter)) {
					let showList = filter.map(item => item[showField]);
					_vm.$message.error(`${ label }：${ showList.join('、') } 已作废，无法${ type === 'confirm' ? '确认' : '反确认' }！`);
					return;
				}
			}
			return selectionList.map(item => $.extend({}, item));
		}
		return undefined;
	},
};
